package com.snpic.appaw.module.insure.util.s3;

import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;

import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class SystemLoadFont {
	/**
	 * 本地读取方法
	 *
	 * @param path     文件路径
	 * @param style    字体样式
	 * @param fontSize 字体大小
	 * @return
	 */
	public static Font styleFont(String path, int style, float fontSize) {
		if (path == null) {
			throw new IllegalArgumentException("Path must not be null");
		}
		Font font = SystemLoadFont.loadStyleFont(path, style, fontSize);// 调用
		return font;
	}

	/**
	 * @param fontFileName 外部字体名
	 * @param style        字体样式
	 * @param fontSize     字体大小
	 * @return
	 */
	public static Font loadStyleFont(String fontFileName, int style, float fontSize) {
		try {
			File file = new File(fontFileName);
			FileInputStream in = new FileInputStream(file);
			Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, in);
			Font dynamicFontPt = dynamicFont.deriveFont(style, fontSize);
			in.close();
			return dynamicFontPt;
		} catch (Exception e) {//异常处理
			e.printStackTrace();
			return new Font("宋体", Font.PLAIN, 20);
		}
	}

	public static void main(String[] args) throws IOException, FontFormatException {
		String simsunPath = "E:\\idea_code\\springboot\\AmazonS3springboot\\src\\main\\resources\\static\\msyh.ttf";
//        String simsunPath = new SystemLoadFont().getClass().getClassLoader().getResource("msyh.ttf").getPath();
		Font font = SystemLoadFont.styleFont(simsunPath, Font.PLAIN, 60f);
		System.out.println(font);


		ClassPathResource classPathResource = new ClassPathResource("font/msyh.ttf");
		InputStream inputStream = classPathResource.getInputStream();
		Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, inputStream).deriveFont(Font.PLAIN, 60f);
		inputStream.close();
		System.out.println(dynamicFont);
	}
}
